<?php

namespace App\Controller\ShengJin;

use App\Base\BaseController4Api;
use App\Entity\ApiMoney;
use App\Entity\ApiUser;
use App\Model\ApiMoneyConfigModel;
use App\Model\ApiMoneyLogModel;
use App\Model\ApiMoneyModel;
use App\Model\ApiMoneyTypeModel;
use App\Model\ApiUserModel;
use Doctrine\DBAL\Query\QueryBuilder;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

use Symfony\Component\HttpFoundation\Request;


/**
 * @route("/api/shengjin")
 */
class ApiMoneyController extends BaseController4Api
{


    /**
     * @route("/daily_unfreeze",name="ApiDailyUnfreeze")
     */
    public function ApiDailyUnfreeze()
    {
//        $projectId = $this->getProjectId();
        $projectId = 2;
        if (!$row = $this->getParamRequired("row")) {$row = 100;}

        $ApiMoneyConfigModel = new ApiMoneyConfigModel($this->getDoctrine());
        $config = $ApiMoneyConfigModel->getByKV("k","PRECENT_UNFREEZE");
        $x = 180;
        if ($config) {
            $x = $config->getV() * 1;
        }

        $conn = $this->myEm()->getConnection();
        $qb = new QueryBuilder($conn);

        //今天凌晨0点的时间戳
        $startTime = strtotime(date($this->YYMMDD));
        $qb->select("A.*,B.addtime")
            ->from("api_money","A")
            ->leftJoin("A","api_money_log","B","A.user_id = B.user_id and B.type = 102 and B.addtime > $startTime")
            ->where("A.project_id = $projectId")
            ->andHaving("isnull(B.addtime)")
            ->andWhere("A.type = 3")
            ->groupBy("A.user_id")
            ->setMaxResults($row)
        ;
        $data = $qb->execute()->fetchAll();
        $ApiMoneyLogModel = new ApiMoneyLogModel($this->getDoctrine());

        foreach ($data as $row) {
            //计算应该减少的积分
            $money = $row["money"] / $x;
            if ($money < 1) {$money = 1;}
            $money = floor($money);

            //增加记录
            $ApiMoneyLogModel->add($projectId,$row["user_id"],3,202,$money * -1,"每日解冻扣除");
            $ApiMoneyLogModel->add($projectId,$row["user_id"],2,102,$money,"每日解冻增加");
        }
        return $this->json_out(1,"处理成功",count($data));

    }



    /**
     * @route("/cash",name="ApiMoneyCash")
     */
    public function ApiMoneyCash()
    {
//        $projectId = $this->getProjectId();
        $projectId = 2;

        if (!$uid = $this->getParamRequired("uid")) {return $this->json_out(0,"请选择用户");}
        if (!$num = $this->getParamRequired("num")) {return $this->json_out(0,"请设置提现金额");}
        $ApiMoneyModel = new ApiMoneyModel($this->getDoctrine());
        $userMOney = $ApiMoneyModel->getMoney($projectId,$uid,2);
        //检查用户余额是否充足
        if ($num > $userMOney->getMoney()) {
            return $this->json_out(0,"提现数量大于余额");
        }

        //取出资金设置
        $ApiMoneyConfigModel = new ApiMoneyConfigModel($this->getDoctrine());
        $conf = $ApiMoneyConfigModel->getByKV("k","JF_CASH_PERCENT");
        $x = 1;if ($conf) {$x = $conf->getV() * 1;}
        $cash = floor($num * $x);
        if ($num<1) {
            return $this->json_out(0,"提现数量过少");
        }

        //查询最后一次赠送
        $ApiMoneyLogModel = new ApiMoneyLogModel($this->getDoctrine());
        $zengsong = $ApiMoneyLogModel->getTermsSum(2,1,2,101,0,time(),0,9999);
        //如果存在系统赠送行为，查看近期消费
        //2=消费积分，205=购物消费
        $xiaofei = $ApiMoneyLogModel->getTermsSum(2,1,2,205,0,time(),0,9999);
        if (abs($xiaofei)<$zengsong) {
            //近期消费金额小于系统赠送金额
            return $this->json_out(0,"余额含有系统赠送积分不能提现，赠送$zengsong,使用$xiaofei;");
        }
        $ApiMoneyLogModel = new ApiMoneyLogModel($this->getDoctrine());
        $error = $ApiMoneyLogModel->add($projectId,$uid,2,204,$num,"提现扣除");
        if ($error) {
            return $this->json_out(0,$error);
        }else{
            return $this->json_out(1,"余额已扣除$num,现金增加$cash",[$num,$cash]);
        }
    }

}
